package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改昵称
 */
public class ChangeNickActivity extends BaseSimpleActivity implements TextWatcher {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.modify_name)
    EditText modifyName;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;

    @Override
    protected int getResouseId() {
        return R.layout.chanegnickname;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("修改昵称");
        layoutTitleBarRightTv.setText("保 存");
        if (!TextUtils.isEmpty(LoginSP.getInstance().getUserName())) {
            modifyName.setText(LoginSP.getInstance().getUserName());
        }
        modifyName.addTextChangedListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.layout_title_bar_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.layout_title_bar_right_tv:
                if (TextUtils.isEmpty(modifyName.getText().toString())) {
                    Helper.showToast("请填写要修改的昵称");
                    return;
                }
                if (modifyName.getText().toString().length() < 4) {
                    Helper.showToast("昵称不能少于4个字符");
                    return;
                }

                CommentModel.getInstant().setUserInfo("", modifyName.getText().toString(), new CallBack() {
                    @Override
                    public void onSuccess(Object o, boolean isSuccess, String msg) {
                        Helper.gotoLogin(isSuccess);//token失效去重新登陆
                        LoginSP.getInstance().setUserName(modifyName.getText().toString());
                        finish();
                    }

                    @Override
                    public void onFailure(String fail) {
                        Helper.showToast(fail);
                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Helper.hideSoftInput(this);
    }

    public static boolean isLetterDigitOrChinese(String str) {
        String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
        return str.matches(regex);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String str = s.toString();
        if (str.length() > 0) {
            String substring = str.substring(str.length() - 1);
            if (!isLetterDigitOrChinese(substring) && !"-".equals(substring)) {
                s.delete(str.length() - 1,str.length());
                Helper.showToast("输入字符不合法");
            }
        }
    }
}
